home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Developers / VideoToolbox / VideoToolboxSources / ReadLuminanceRecord.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-15  |  6.6 KB  |  187 lines  |  [TEXT/CWIE]

  1. /*
  2. ReadLuminanceRecord.c
  3.  
  4. long InitializeLuminanceRecord(LuminanceRecord *LP,short flags);
  5.  
  6.     i=ReadLuminanceRecord("LuminanceRecord1.h",&LR,0);
  7. Reads a LuminanceRecord?.h file at runtime. In the past, these calibration-data files
  8. could only be used by #including them at compile time.
  9.  
  10.     long WriteLuminanceRecord(char *filename,LuminanceRecord *LP,short flags);
  11. Writes (and verifies) a LuminanceRecord, appending to the file.
  12.  
  13.     Description *DescribeLuminanceRecord(LuminanceRecord *LP);
  14. Creates a Description array to read and write LuminanceRecord assignment files.
  15.  
  16.  
  17. HISTORY:
  18. 7/29/91 dgp
  19. 8/24/91    dgp    Made compatible with THINK C 5.0.
  20.             Preserve default values of LP->VMin and LP->VMax if no new values are read.
  21. 8/26/91    dgp    Rewrote using new SetVariable() routine, which makes the code easier to
  22.             read. 
  23. 12/17/92 dgp Added dacSize.
  24. 12/21/92 dgp Changed type of dacSize from long to short.
  25. 6/3/93    dgp    Updated to work with new Assign.c. 
  26.             Created DescribeLuminanceRecordAssignment() and WriteLuminanceRecord().
  27. 8/12/93    dgp    Renamed routines to be consistent with new Assign.c.
  28.             Renamed "DescribeLuminanceRecordAssignment" to "DescribeLuminanceRecord".
  29. 3/4/94    dgp ReadLuminanceRecord no longer initializes. Added InitializeLuminanceRecord.
  30. 8/15/95 dgp fixed typo in error message string reported by Bosco.
  31. */
  32.  
  33. #include "VideoToolbox.h"
  34. #include "Luminance.h"
  35. #define VARIABLES 32
  36. long InitializeLuminanceRecord(LuminanceRecord *LP,short flags);
  37.  
  38. long InitializeLuminanceRecord(LuminanceRecord *LP,short flags)
  39. {
  40.     Description *d;
  41.     long n;
  42.  
  43.     d=DescribeLuminanceRecord(LP);
  44.     if(d==NULL){
  45.         if(flags&assignNoPrintfExit)return assignMemoryError;
  46.         else PrintfExit("\nInitializeLuminanceRecord: no room for Variables.\n\007");
  47.     }
  48.     n=InitializeDescribedVars(d,flags);
  49.     LP->dacSize=8;                    /*  default value */
  50.     free(d);
  51.     return n;
  52. }
  53.  
  54. long ReadLuminanceRecord(char *filename,LuminanceRecord *LP,short flags)
  55. {
  56.     Description *d;
  57.     long n;
  58.  
  59.     assert(LP!=NULL);
  60.     d=DescribeLuminanceRecord(LP);
  61.     if(d==NULL){
  62.         if(flags&assignNoPrintfExit)return assignMemoryError;
  63.         else PrintfExit("\nReadLuminanceRecord: no room for Variables.\n\007");
  64.     }
  65. //    n=InitializeDescribedVars(d,flags);
  66.     LP->dacSize=8;                    /*  default value */
  67.     n=ReadAssignmentFile(filename,d,flags);
  68.     free(d);
  69.     return n;
  70. }
  71.  
  72. long WriteLuminanceRecord(char *filename,LuminanceRecord *LP,short flags)
  73. {
  74.     Description *d=NULL,*d2=NULL;
  75.     LuminanceRecord *LP2=NULL;
  76.     long n,m;
  77.  
  78.     /*  Write */
  79.     assert(LP!=NULL);
  80.     d=DescribeLuminanceRecord(LP);
  81.     if(d==NULL){
  82.         n=assignMemoryError;
  83.         if(flags&assignNoPrintfExit)goto done;
  84.         else PrintfExit("\nWriteLuminanceRecord: no room for Variables.\n\007");
  85.     }
  86.     n=PrintAssignmentsToFile(filename,d,flags);
  87.     SetFileInfo(filename,'TEXT','KAHL');
  88.     if(n<0)goto done;
  89.     
  90.     /*  Verify */
  91.     LP2=(LuminanceRecord *)malloc(sizeof(LuminanceRecord));
  92.     if(LP2==NULL){
  93.         n=assignMemoryError;
  94.         if(flags&assignNoPrintfExit)goto done;
  95.         else PrintfExit("\nWriteLuminanceRecord: no room for LuminanceRecord.\n\007");
  96.     }
  97.     d2=DescribeLuminanceRecord(LP2);
  98.     if(d2==NULL){
  99.         n=assignMemoryError;
  100.         if(flags&assignNoPrintfExit)goto done;
  101.         else PrintfExit("\nWriteLuminanceRecord: no room for Variables.\n\007");
  102.     }
  103.     m=ReadAssignmentFile(filename,d2,flags|assignReportUnknown);
  104.     if(m<0){
  105.         n=m;
  106.         goto done;
  107.     }
  108.     m=UnequalDescribedVars(d,d2,flags);
  109.     if(m<0){
  110.         n=m;
  111.         goto done;
  112.     }
  113. done:
  114.     if(d2!=NULL)free(d2);
  115.     if(LP2!=NULL)free(LP2);
  116.     if(d!=NULL)free(d);
  117.     return n;
  118. }
  119.  
  120. Description *DescribeLuminanceRecord(LuminanceRecord *LP)
  121. {
  122.     Description *d;
  123.     int j;
  124.  
  125.     d=(Description *)malloc(VARIABLES*sizeof(Description));
  126.     if(d==NULL)return NULL;
  127.     j=0;
  128.     d[j++]=Describe(shortType,&LP->screen,"LR.screen"
  129.         ,"device=GetScreenDevice(LR.screen);");
  130.     d[j++]=Describe(stringType,&LP->id,"LR.id",NULL);
  131.     d[j++]=Describe(stringType,&LP->name,"LR.name",NULL);
  132.     d[j++]=Describe(stringType,&LP->date,"LR.date",NULL);
  133.     d[j++]=Describe(stringType,&LP->notes,"LR.notes",NULL);
  134.     d[j++]=Describe(shortType,&LP->dacSize,"LR.dacSize",NULL);
  135.     d[j++]=Describe(doubleType,&LP->LMin,"LR.LMin","luminance at VMin");
  136.     d[j++]=Describe(doubleType,&LP->LMax,"LR.LMax"
  137.         ,"luminance at VMax");
  138.     d[j++]=Describe(doubleType,&LP->LBackground,"LR.LBackground"
  139.         ,"background luminance during calibration");
  140.     d[j++]=Describe(shortType,&LP->VBackground,"LR.VBackground"
  141.         ,"background number used during calibration");
  142.     d[j++]=Describe(doubleType,&LP->dpi,"LR.dpi","pixels/inch");
  143.     d[j++]=Describe(doubleType,&LP->Hz,"LR.Hz","frames/second");
  144.     d[j++]=Describe(stringType,&LP->units,"LR.units",NULL);
  145.     d[j++]=Describe(longType,&LP->coefficients,"LR.coefficients"
  146.         ,"# of coefficients in polynomial fit");
  147.     d[j++]=DescribeArray(doubleType,&LP->p,"LR.p"
  148.         ,"L(V)=p[0]+p[1]*V+p[2]*V*V+ . . . ±polynomialError"
  149.         ,sizeof(LP->p)/sizeof(LP->p[0]),0L);
  150.     d[j++]=Describe(doubleType,&LP->polynomialError,"LR.polynomialError"
  151.         ,"RMS error of fit");
  152.     d[j++]=DescribeArray(doubleType,&LP->q,"LR.q"
  153.         ,"L(V)=q[0]+q[1]*V+q[2]*V*V±quadraticError",sizeof(LP->q)/sizeof(LP->q[0]),0L);
  154.     d[j++]=Describe(doubleType,&LP->quadraticError,"LR.quadraticError"
  155.         ,"RMS error of fit");
  156.     d[j++]=DescribeArray(doubleType,&LP->power,"LR.power"
  157.         ,"L(V)=power[0]+Rectify(power[1]+power[2]*V)^power[3]±powerError */\\\n"
  158.         "    /* where Rectify(x)=x if x≥0, and Rectify(x)=0 otherwise. */\\\n"
  159.         "    /* Pelli & Zhang (1991) Eqs.9&10 use symbols v=V/255, */\\\n"
  160.         "    /* alpha=power[0], beta=power[1], kappa=power[2]*255, gamma=power[3]"
  161.         ,sizeof(LP->power)/sizeof(LP->power[0]),0L);
  162.     d[j++]=Describe(doubleType,&LP->powerError,"LR.powerError"
  163.         ,"RMS error of fit");
  164.     d[j++]=DescribeArray(doubleType,&LP->fixedPower,"LR.fixedPower"
  165.         ,"L(V)=fixedPower[0]+Rectify(fixedPower[1]+fixedPower[2]*V)^fixedPower[3]±fixedPowerError */\\\n"
  166.         "    /* The exponent fixedPower[3] is fixed."
  167.         ,sizeof(LP->fixedPower)/sizeof(LP->fixedPower[0]),0L);
  168.     d[j++]=Describe(doubleType,&LP->fixedPowerError,"LR.fixedPowerError"
  169.         ,"RMS error of fit");
  170.     d[j++]=Describe(doubleType,&LP->r,"LR.r",NULL);
  171.     d[j++]=Describe(doubleType,&LP->g,"LR.g",NULL);
  172.     d[j++]=Describe(doubleType,&LP->b,"LR.b",NULL);
  173.     d[j++]=Describe(doubleType,&LP->gainAccuracy,"LR.gainAccuracy",NULL);
  174.     d[j++]=Describe(doubleType,&LP->gm,"LR.gm"
  175.         ,"The monitor's contrast gain.");
  176.     d[j++]=Describe(shortType,&LP->rangeSet,"LR.rangeSet"
  177.         ,"zero indicates that range parameters have yet to be set");
  178.     d[j++]=Describe(shortType,&LP->L.exists,"LR.L.exists"
  179.         ,"zero indicates that luminance table has yet to be initialized");
  180.     d[j++]=Describe(shortType,&LP->VMin,"LR.VMin"
  181.         ,"minimum value that can be loaded into DAC");
  182.     d[j++]=Describe(shortType,&LP->VMax,"LR.VMax"
  183.         ,"maximum value that can be loaded into DAC");
  184.     d[j++]=Describe(0,NULL,NULL,NULL);                /* Mark end of list */
  185.     assert(j<=VARIABLES);
  186.     return d;
  187. }